/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myxa;

import com.ibm.db2.jcc.DB2XADataSource;
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.sql.XADataSource;

/**
 *
 * @author Tuandn
 */
public class DataSourceFactory {

    public static final String MSSQL_PASSWORD = "sa";
    public static final String MSSQL_USERNAME = "sa";
    public static final String DB2_PASSWORD = "db2admin";
    public static final String DB2_USERNAME = "db2admin";

    public static DataSource getMSSQLDataSource() {

        SQLServerXADataSource xaDS = new SQLServerXADataSource();
        xaDS.setDatabaseName("demospring");
        xaDS.setServerName("tuan-pc");
        xaDS.setPortNumber(1433);
        xaDS.setSelectMethod("cursor");
        return xaDS;
    }

    public static XADataSource getDB2DataSource() {

        DB2XADataSource xaDS = new DB2XADataSource();
        xaDS.setDatabaseName("EXPRESS");
        xaDS.setServerName("tuan-pc");
        xaDS.setPortNumber(50000);
        return xaDS;
    }

    /**
     * test connection to the db2 server
     * @param args
     */
    public static void testDB2Connection() {
        Connection con = null;
        try {
            final String url = "jdbc:db2://TUAN-PC:50000/EXPRESS";
            //use type 4
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            con = DriverManager.getConnection(url, DB2_USERNAME, DB2_PASSWORD);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from sqlj.customer");
            while (rs.next()) {
                System.out.println("id: " + rs.getInt("id") + ", fullname: " + rs.getString("fullname"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(DataSourceFactory.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
